package com.gzsxy.esjy.service.auth.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gzsxy.esjy.bus.log.annotation.OperLog;
import com.gzsxy.esjy.bus.log.enum1.LogActionType;
import com.gzsxy.esjy.service.auth.entity.vo.UserQueryVo;
import com.gzsxy.esjy.service.auth.service.UserService;
import com.gzsxy.esjy.service.base.dto.request.UserRequest;
import com.gzsxy.esjy.service.base.dto.response.DefaultResponse;
import com.gzsxy.esjy.service.base.dto.response.UserRsp;
import com.gzsxy.esjy.service.base.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 后台用户
 * @date 2022/3/1 21:36
 */
@CrossOrigin  //解决跨域
@RefreshScope    //动态刷新配置文件-外部配置文件
@RestController
@RequestMapping("/admin/user")
@Api("用户管理")
public class UserController {


    @Autowired
    private UserService userService;


    @GetMapping("/list")
    @ApiOperation("查询所有用户")
    public DefaultResponse list(Page<?> page, UserQueryVo userQueryVo){
        IPage<UserRsp> userRspList =  userService.getPageVo(page,userQueryVo);
        return DefaultResponse.ok(userRspList);
    }

    @PutMapping("/editFreeze/{id}")
    @ApiOperation("修改账户状态")
    @OperLog(operModul = "用户管理",operType = LogActionType.UPDATE,operDesc = "修改账户状态")
    public DefaultResponse editFreeze(@PathVariable String id,@RequestBody UserRequest userRequest){
        userService.verify(id,userRequest.getFreeze());
        return DefaultResponse.ok();
    }

    @PutMapping("/edit")
    @ApiOperation("修改用户")
    @OperLog(operModul = "用户管理",operType = LogActionType.UPDATE,operDesc = "修改用户")
    public DefaultResponse editUser(@RequestBody UserRequest userRequest){
        UserRsp userRsp =userService.editUser(userRequest);
        return DefaultResponse.ok(userRsp);
    }


    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除用户")
    @OperLog(operModul = "用户管理",operType = LogActionType.DELETE,operDesc = "删除用户")
    public DefaultResponse deleteUser(@PathVariable String id){
        userService.deleteUser(id);
        return DefaultResponse.ok();
    }


    @GetMapping("/getNumByUser")
    @ApiOperation("根据当天日期查看注册用户人数")
    public DefaultResponse selectRegisterNumByDay(@RequestParam String day){
        Integer count = userService.selectRegisterNumByDay(day);
        return DefaultResponse.ok(count);
    }


}
